package cn.watson.org.controller;

import cn.watson.basic.utils.AjaxResult;
import cn.watson.basic.utils.PageList;
import cn.watson.org.domain.Employee;
import cn.watson.org.query.EmployeeQuery;
import cn.watson.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Title:
* @Description: 员工接口类
* @Author: ZhangRui
* @Version: 1.0
* @Date:  2022/6/24 10:10
* @Parameters:
* @Return
*/
@RestController
// 此注解是@Controller+@ResponseBody两个注解的复合注解，这样整个接口类的所有接口方法全都是返回的JSON数据
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired // 注入
    private IEmployeeService employeeService;

    /**
    * @Title: loadById
    * @Description: 更具id查询
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/24 10:00
    * @Parameters: [id]
    * @Return cn.watson.org.domain.Employee
    */
    @GetMapping("/{id}")
    @ResponseBody   // 将前端传递的JSON对象数据转换为Java对象数据，
    public Employee loadById(@PathVariable("id") Long id){ // @PathVariable可以获取到接口路径中使用{}的占位符参数
        return employeeService.loadById(id);
    }





    /**
    * @Title: remove
    * @Description: 更具id删除
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/24 11:54
    * @Parameters: [id]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("我很忙的，等下再点！！");
        }
    }

    /**
    * @Title: addOpUpt
    * @Description: 新增或者修改
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/24 12:37
    * @Parameters: [employee]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
                System.out.println(employeeService);
            }else {
                employeeService.update(employee);
                System.out.println(employeeService);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/24 12:41
    * @Parameters: []
    * @Return java.util.List<cn.watson.org.domain.Employee>
    */
    @GetMapping
    public List<Employee> loadAll(){
        return employeeService.loadAll();
    }


    /**
    * @Title: queryData
    * @Description: 高级加分页查询
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/24 12:44
    * @Parameters: [employeeQuery]
    * @Return cn.watson.basic.utils.PageList<cn.watson.org.domain.Employee>
    */
    @PostMapping
    public PageList<Employee> queryData(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> data = employeeService.queryData(employeeQuery);
        return data;
    }

    /**
    * @Title: patchDel
    * @Description: 批量删除
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/6/26 11:24
    * @Parameters: [ids]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            employeeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙请重试！！！");
        }
    }

}
